package com.growthsee.web.controller.system;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.growthsee.common.constant.Constants;
import com.growthsee.common.core.domain.AjaxResult;
import com.growthsee.common.core.domain.entity.SysMenu;
import com.growthsee.common.core.domain.entity.SysUser;
import com.growthsee.common.core.domain.model.LoginBody;
import com.growthsee.common.core.domain.model.LoginUser;
import com.growthsee.common.core.domain.model.TokenParam;
import com.growthsee.common.utils.ServletUtils;
import com.growthsee.common.utils.StringUtils;
import com.growthsee.common.utils.bean.BeanUtils;
import com.growthsee.framework.web.service.SysLoginService;
import com.growthsee.framework.web.service.SysPermissionService;
import com.growthsee.framework.web.service.TokenService;
import com.growthsee.rider.domain.RiderInfo;
import com.growthsee.rider.domain.dto.RiderInfoDto;
import com.growthsee.rider.mapper.RiderInfoMapper;
import com.growthsee.rider.wrapper.RiderWrapper;
import com.growthsee.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author growthsee
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RiderInfoMapper riderInfoMapper;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody){
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @PostMapping("/token")
    public AjaxResult token(@RequestBody TokenParam tokenParam){
        if(StringUtils.isEmpty(tokenParam.getAppKey()))return AjaxResult.error("appKey 不能为空");
        if(StringUtils.isEmpty(tokenParam.getSecret()))return AjaxResult.error("secret 不能为空");
        String token = loginService.token(tokenParam.getAppKey(),tokenParam.getSecret());
        if(StringUtils.isEmpty(token))return AjaxResult.error("appKey或者secret不正确");
        return AjaxResult.success(null,token);
    }




    @GetMapping("wx/login")
    public AjaxResult wxLogin(@RequestParam("code")String code,@RequestParam("state")String state){
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.wxLogin(code,state);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }
    @GetMapping("/getRider")
    public AjaxResult getRider() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        RiderInfo riderInfo =riderInfoMapper.selectOne(Wrappers.<RiderInfo>lambdaQuery().eq(RiderInfo::getOpenId,loginUser.getUser().getUserName()));
        RiderInfoDto riderInfoDto = new RiderInfoDto();
        BeanUtils.copyBeanProp(riderInfoDto,riderInfo);
        RiderWrapper.builder().propertyHandler(Arrays.asList(riderInfoDto));
        return AjaxResult.success(riderInfoDto);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
